package com.iyundao.io.mapper;

import com.iyundao.io.model.ActivityModelLookBack;
import com.iyundao.io.tools.ActivityModelVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ActivityModelLookBackMapper {
    @Insert({
        "insert into activitymodellookback (id, activitytitle, ",
        "activitycontent, activitystarttime, ",
        "activityendtime, activitysignstarttime, ",
        "activitysignendtime, activitylongitude, ",
        "activitylatitude, activitysitename, ",
        "activitycreatetime, activitynumber, ",
        "activityjoinnumber, activityintegral, ",
        "activityimg, userid, ",
        "activityflag, activityid, ",
        "activitydigest, organizationid)",
        "values (#{id,jdbcType=INTEGER}, #{activitytitle,jdbcType=VARCHAR}, ",
        "#{activitycontent,jdbcType=VARCHAR}, #{activitystarttime,jdbcType=TIMESTAMP}, ",
        "#{activityendtime,jdbcType=TIMESTAMP}, #{activitysignstarttime,jdbcType=TIMESTAMP}, ",
        "#{activitysignendtime,jdbcType=TIMESTAMP}, #{activitylongitude,jdbcType=DOUBLE}, ",
        "#{activitylatitude,jdbcType=DOUBLE}, #{activitysitename,jdbcType=VARCHAR}, ",
        "#{activitycreatetime,jdbcType=TIMESTAMP}, #{activitynumber,jdbcType=INTEGER}, ",
        "#{activityjoinnumber,jdbcType=INTEGER}, #{activityintegral,jdbcType=INTEGER}, ",
        "#{activityimg,jdbcType=VARCHAR}, #{userid,jdbcType=VARCHAR}, ",
        "#{activityflag,jdbcType=INTEGER}, #{activityid,jdbcType=INTEGER}, ",
        "#{activitydigest,jdbcType=VARCHAR}, #{organizationid,jdbcType=VARCHAR})"
    })
    int insert(ActivityModelLookBack record);

    @InsertProvider(type=ActivityModelLookBackSqlProvider.class, method="insertSelective")
    int insertSelective(ActivityModelLookBack record);

    @Select("select a.id, a.activitytitle,a.activitycontent,DATE_FORMAT(a.activitystarttime,'%Y-%m-%d %H:%i:%s') activitystarttime,DATE_FORMAT(a.activityendtime,'%Y-%m-%d %H:%i:%s') activityendtime, " +
            "DATE_FORMAT(a.activitysignstarttime,'%Y-%m-%d %H:%i:%s') activitysignstarttime, DATE_FORMAT(a.activitysignendtime,'%Y-%m-%d %H:%i:%s') activitysignendtime, a.activitylongitude, a.activitylatitude, " +
            "a.activitysitename, DATE_FORMAT(a.activitycreatetime,'%Y-%m-%d %H:%i:%s') activitycreatetime, a.activitynumber, a.activityjoinnumber, a.activityintegral, " +
            "a.activityimg, a.userid, a.activityflag, a.activityid, a.activitydigest, a.organizationid,ac.categoryname,ac.categoryintegral" +
            " from activitymodellookback a " +
            "INNER JOIN activitycategory ac " +
            " on a.activityid= ac.id " +
            "ORDER BY a.id DESC")
    List<ActivityModelVo> selectAll();

    @Select("select a.id, a.activitytitle,a.activitycontent,DATE_FORMAT(a.activitystarttime,'%Y-%m-%d %H:%i:%s') activitystarttime,DATE_FORMAT(a.activityendtime,'%Y-%m-%d %H:%i:%s') activityendtime, " +
            "DATE_FORMAT(a.activitysignstarttime,'%Y-%m-%d %H:%i:%s') activitysignstarttime, DATE_FORMAT(a.activitysignendtime,'%Y-%m-%d %H:%i:%s') activitysignendtime, a.activitylongitude, a.activitylatitude, " +
            "a.activitysitename, DATE_FORMAT(a.activitycreatetime,'%Y-%m-%d %H:%i:%s') activitycreatetime, a.activitynumber, a.activityjoinnumber, a.activityintegral, " +
            "a.activityimg, a.userid, a.activityflag, a.activityid, a.activitydigest, a.organizationid,ac.categoryname,ac.categoryintegral" +
            " from activitymodellookback a " +
            "INNER JOIN activitycategory ac " +
            " on a.activityid= ac.id " +
            "where a.organizationid in ( " +
            "SELECT convert(sid,CHAR(36)) FROM sys_dept " +
            "where sid= #{sid,jdbcType=CHAR} " +
            "or " +
            "parent_id= #{sid,jdbcType=CHAR} )" +
            "ORDER BY a.id DESC")
    List<ActivityModelVo> selectAllBySid(String sid);

    @Update("update activitymodellookback set activityflag=1 where id=#{id,jdbcType=INTEGER}")
    int updateFlag(Integer id);
}